package com.carrental.finance.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.carrental.common.result.Result;
import com.carrental.finance.dto.PaymentDTO;
import com.carrental.finance.entity.Payment;
import com.carrental.finance.service.PaymentService;
import com.carrental.finance.vo.PaymentVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 支付控制器
 */
@Api(tags = "支付管理")
@RestController
@RequestMapping("/payments")
public class PaymentController {

    @Autowired
    private PaymentService paymentService;

    @ApiOperation("创建支付记录")
    @PostMapping
    public Result<Long> createPayment(@RequestBody @Validated PaymentDTO paymentDTO) {
        Long id = paymentService.createPayment(paymentDTO);
        return Result.success(id);
    }

    @ApiOperation("根据ID获取支付详情")
    @GetMapping("/{id}")
    public Result<PaymentVO> getPaymentById(@PathVariable Long id) {
        PaymentVO paymentVO = paymentService.getPaymentById(id);
        return Result.success(paymentVO);
    }

    @ApiOperation("根据支付单号获取支付详情")
    @GetMapping("/no/{paymentNo}")
    public Result<PaymentVO> getPaymentByPaymentNo(@PathVariable String paymentNo) {
        PaymentVO paymentVO = paymentService.getPaymentByPaymentNo(paymentNo);
        return Result.success(paymentVO);
    }

    @ApiOperation("根据订单ID获取支付详情列表")
    @GetMapping("/order/{orderId}")
    public Result<Page<PaymentVO>> getPaymentsByOrderId(
            @PathVariable Long orderId,
            @ApiParam("页码") @RequestParam(defaultValue = "1") Integer current,
            @ApiParam("每页大小") @RequestParam(defaultValue = "10") Integer size) {
        Page<Payment> page = new Page<>(current, size);
        Page<PaymentVO> result = paymentService.getPaymentsByOrderId(orderId, page);
        return Result.success(result);
    }

    @ApiOperation("根据用户ID分页查询支付记录")
    @GetMapping("/user/{userId}")
    public Result<Page<PaymentVO>> pageUserPayments(
            @PathVariable Long userId,
            @ApiParam("页码") @RequestParam(defaultValue = "1") Integer current,
            @ApiParam("每页大小") @RequestParam(defaultValue = "10") Integer size) {
        Page<Payment> page = new Page<>(current, size);
        Page<PaymentVO> result = paymentService.pageUserPayments(userId, page);
        return Result.success(result);
    }

    @ApiOperation("分页查询支付记录")
    @GetMapping("/page")
    public Result<Page<PaymentVO>> pagePayments(
            @ApiParam("页码") @RequestParam(defaultValue = "1") Integer current,
            @ApiParam("每页大小") @RequestParam(defaultValue = "10") Integer size,
            @ApiParam("支付单号") @RequestParam(required = false) String paymentNo,
            @ApiParam("订单号") @RequestParam(required = false) String orderNo,
            @ApiParam("用户ID") @RequestParam(required = false) Long userId,
            @ApiParam("支付类型") @RequestParam(required = false) Integer paymentType,
            @ApiParam("支付状态") @RequestParam(required = false) Integer status,
            @ApiParam("开始时间") @RequestParam(required = false) String startTime,
            @ApiParam("结束时间") @RequestParam(required = false) String endTime) {
        Page<Payment> page = new Page<>(current, size);
        Page<PaymentVO> result = paymentService.pagePayments(page, paymentNo, orderNo, userId, paymentType, status, startTime, endTime);
        return Result.success(result);
    }

    @ApiOperation("处理支付回调")
    @PostMapping("/callback")
    public Result<Boolean> handlePaymentCallback(
            @ApiParam("支付单号") @RequestParam String paymentNo,
            @ApiParam("交易流水号") @RequestParam String transactionId,
            @ApiParam("支付状态") @RequestParam Integer status) {
        boolean success = paymentService.handlePaymentCallback(paymentNo, transactionId, status);
        return Result.success(success);
    }

    @ApiOperation("取消支付")
    @PostMapping("/cancel/{id}")
    public Result<Boolean> cancelPayment(@PathVariable Long id) {
        boolean success = paymentService.cancelPayment(id);
        return Result.success(success);
    }
} 